package love.yuanmengrobot.qianyugarden.service.impl;

import love.yuanmengrobot.qianyugarden.dao.*;
import love.yuanmengrobot.qianyugarden.entity.*;
import love.yuanmengrobot.qianyugarden.service.UserEventsService;
import love.yuanmengrobot.qianyugarden.vo.MyGarden;
import love.yuanmengrobot.qianyugarden.vo.UserEventYuanwenHistoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (UserEvents)表服务实现类
 *
 * @author makejava
 * @since 2021-07-13 14:28:15
 */
@Service
public class UserEventsServiceImpl implements UserEventsService {
    @Autowired
    UserEventsDao userEventsDao;

    @Autowired
    PersonalMenuDao personalMenuDao;

    @Autowired
    MenuItemDao menuItemDao;

    @Autowired
    YuanwenDao yuanwenDao;

    @Autowired
    BookDao bookDao;

    @Autowired
    UserDao userDao;
    /**
     * 通过ID查询单条数据
     *
     * @param objectid 主键
     * @return 实例对象
     */
    @Override
    public UserEvents queryById(String objectid) {
        return this.userEventsDao.queryById(objectid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<UserEvents> queryAllByLimit(int offset, int limit) {
        return this.userEventsDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param userEvents 实例对象
     * @return 实例对象
     */
    @Override
    public UserEvents insert(UserEvents userEvents) {
        this.userEventsDao.insert(userEvents);
        return userEvents;
    }

    /**
     * 修改数据
     *
     * @param userEvents 实例对象
     * @return 实例对象
     */
    @Override
    public UserEvents update(UserEvents userEvents) {
        this.userEventsDao.update(userEvents);
        return this.queryById(userEvents.getObjectid());
    }

    /**
     * 通过主键删除数据
     *
     * @param objectid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String objectid) {
        return this.userEventsDao.deleteById(objectid) > 0;
    }

    @Override
    public MyGarden loadMyGarden(String id) {



        MyGarden myGarden = new MyGarden();

        /*加载个人园子菜单*/
        myGarden.setPersonalMenu(personalMenuDao.queryById(id));

        /*加载园子菜单项*/
        myGarden.setMenuItemList(menuItemDao.queryAll(new MenuItem()));

        /*加载五项*/
        UserEvents follow = new UserEvents();
        follow.setUserid(id);
        follow.setEventtype("关注");
        myGarden.setFollowNum(userEventsDao.queryCountByUserEvents(follow));

        UserEvents fans = new UserEvents();
        fans.setTargetid(id);
        fans.setEventtype("关注");
        myGarden.setFansNum(userEventsDao.queryCountByUserEvents(follow));

        UserEvents liked = new UserEvents();
        liked.setUserid(id);
        liked.setEventtype("点赞");
        myGarden.setLikedNum(userEventsDao.queryCountByUserEvents(liked));

        Yuanwen yuanwen = new Yuanwen();
        yuanwen.setUserid(id);
        System.out.println("发现ID："+id);
        yuanwen.setStatus("发布");
        myGarden.setReleaseNum(yuanwenDao.queryCountByYuanwen(yuanwen));

        Book book = new Book();
        book.setUserid(id);
        book.setStatus("发布");
        myGarden.setUploadNum(bookDao.queryCountByBook(book));

        /*加载动态*/
        myGarden.setBookList(bookDao.queryLatest(3));
        myGarden.setYuanwenList(yuanwenDao.queryLatest(3));

        User user=userDao.queryById(id);
        myGarden.setUser(user);
        return myGarden;
    }

    @Override
    public List<UserEventYuanwenHistoryVo> queryAllYuanWenHistoryByLimit(String objectid ,int offset, int limit) {


        return this.yuanwenDao.queryAllYuanWenHistoryByLimit(objectid,offset,limit);
    }


}
